import { _decorator, Component, Label, Node, PageView } from 'cc';
import { UIBase } from '../../FrameWork3.8.6/FrameWork/UIFramework/UIBase';
import { LevelDtMgr } from '../../FrameWork3.8.6/FrameWork/Data/DataBaseMgr';
import { DataMgr } from '../../FrameWork3.8.6/FrameWork/Data/DataMgr';
import { LevelDt } from '../../FrameWork3.8.6/FrameWork/Common/CommonInterface';
import { BundleConfig } from '../../FrameWork3.8.6/FrameWork/NeedToChange/BundleConfig';
const { ccclass, property } = _decorator;

export const LevelIdToIndex={
    "_Level1-1":0,
    "_Level1-2":1,
    "_Level1-3":2,
    "_Level1-4":3,
    "_Level1-5":4,
}
@ccclass('LevelPanel')
export class LevelPanel extends UIBase {
    //关卡数据管理者
    private _levelDtMgr:LevelDtMgr=null;

    //页面视图节点
    private _pageView:PageView=null;
    onInit(...rest: any[]): void {
        //初始化关卡数据管理者
        this._levelDtMgr=DataMgr.instance.getMgr<LevelDtMgr>("LevelDtMgr");
        //初始化页面视图节点
        this._pageView=this.getComp<PageView>("_LevelChange","PageView");
        //注册关闭按钮点击事件
        this.addClickEventListener("_CloseBtn",()=>{
            this.closeUI(this.uiName);
        })
        this.initData();

    }


    //给所有关卡按钮初始化添加点击事件并且通过关卡数据配置对应的关卡数据
    initData(){
        //let arrBtnPanrent:Node[]=[];
        let AllLevelDt=this._levelDtMgr.getAllDatas<LevelDt>();
        //将所有关卡按钮的父亲放到一个数组中
        for(let i=1;i<=AllLevelDt.length;i++){
            let node=this.getNode("_LevelBtn"+i);
            //获取当前页面对应的数据
            let curLevelDt=this._levelDtMgr.getDataByID<LevelDt>(7000+i);
            let curPropID=curLevelDt.propID;

            //先通过ID获取当前页面节点
            let allPanel=this._pageView.getPages();
            let curPanel=allPanel[i-1];
            //初始化当前页面的信息
            let levelNameLabel=curPanel.getChildByName("_LevelNameLabel");
            levelNameLabel.getComponent(Label).string=curLevelDt.screenName;
            //获取当前幕的关卡数量的数组
            let arrLevelDetails=curLevelDt.arrLevelDetails;
            //遍历当前页面的按钮添加点击事件
            for(let child of node.children){
                this.addClickEventListener(child.name,()=>{
                    //拿到当前关卡的详细信息
                    let levelDetails=arrLevelDetails[LevelIdToIndex[child.name]];
                    //弹出该关卡详细信息的界面，并且传出该关卡的数据信息
                    this.openUI("LevelDetailsPanel",false,false,BundleConfig.UI.name,"Normal",levelDetails,curPropID);
                })
            }
        }

        

    }
}


